package com.file;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Base64;

public class FileInputWhie {
    public void myReadFilebyWhie() {
        StringBuffer stringBuffer = new StringBuffer();
        File descFile = new File("d:"+File.separator + "FileIo" +File.separator+ "artitle.txt");
        if (descFile.exists()) {
            try {
                InputStream is = new FileInputStream(descFile);
                byte[] data = new byte[8];
                int len = 0;
//                while ((len = is.read(data)) !=-1) {
//                    stringBuffer.append(new String(data,0,len));
//                }

                //解决中文乱码
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));
                String line;
                while ((line = reader.readLine()) != null){
                    stringBuffer.append(line);
                    System.out.println(line);
                }
                System.out.println("读取的内容==》" + stringBuffer);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件不存在");
        }
    }

    //使用字符流方式

    public void myReader() {
        StringBuffer stringBuffer = new StringBuffer();
        File descFile = new File("d:"+File.separator + "FileIo" +File.separator+ "artitle.txt");
        if (descFile.exists()) {
            try {
                Reader is = new FileReader(descFile);
                char[] data = new char[2048];
                int len =  is.read(data);
                    stringBuffer.append(new String(data,0,len));
                System.out.println("读取的内容==》" + stringBuffer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件不存在");
        }
    }

    public void encodePic() throws Exception {
        File imgFile = new File("D:" + File.separator + "FileIo" + File.separator + "panda.png");
        File destFile = new File("D:" + File.separator + "FileIo" + File.separator + "201401059name.txt");

        InputStream is = new FileInputStream(imgFile);
        byte[] data = new byte[1024];
        is.read(data);
        System.out.println("原始数据data=" + data);
        byte[] enData = Base64.getEncoder().encode(data);
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        OutputStream os = new FileOutputStream(destFile);
        os.write(enData);
    }

    public void ReaderByStream() {
        File descFile = new File("d:"+File.separator + "FileIo" +File.separator+ "out1.txt");
        if (!descFile.exists()) {
            try {
                descFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream(descFile));

            writer.append("这是通过转换方式写入的内容");
            writer.append("99999996hfdhfih");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void baseImg() {
        File orgImg = new File("d:" + File.separator+"FileIo" + File.separator + "panda.png");
        File destFile = new File("d:" + File.separator+"FileIo" + File.separator + "201401059-1.txt");
        try {
            InputStream is = new FileInputStream(orgImg);
            byte[] data = new byte[2048];
            is.read(data);

            //Base64
           byte[] enData =  Base64.getEncoder().encode(data);
           if (!destFile.exists()) {
               destFile.createNewFile();
           }
           OutputStream os = new FileOutputStream(destFile);
           os.write(enData);
            System.out.println("写入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void writerByChar() {
        File desFile = new File("d:" + File.separator+"FileIo" + File.separator + "writerout.txt");
        if (!desFile.exists()){
            try {
                desFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            Writer writer = new FileWriter(desFile);
            writer.write("这是通过writer 字符流方式写入文件");
            writer.append("哈哈哈哈");
            writer.flush();
            writer.close();
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readByChar() {
        File desFile = new File("d:" + File.separator+"FileIo" + File.separator + "writerout.txt");
        if (desFile.exists()) {
            try {
                Reader reader = new FileReader(desFile);
                char[] readData = new char[1024];
                int len = reader.read(readData);
                System.out.println(new String(readData,0,len));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
